<script setup>
import { ref } from 'vue';
import { userRegisterAPI } from '@/apis/user'




//控制注册与登录表单的显示， 默认显示注册
const isRegister = ref(false)

//定义数据模型
const registerData = ref(
  {
    username:'',
    password:'',
    rePassword:''
  }
)
const loginData = ref(
  {
    username:'',
    password:''
  }
)
//校验密码的函数
const checkRePassword = (rule,value,callback) =>{
  if(value===''){
    callback(new Error('请再次输入密码'))
  }else if(value !== registerData.value.password){
    callback(new Error('两次输入密码不一致'))
  }else{
    callback()
  }
}
//定义表单校验规则
const rules={
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 5, max: 16, message: '请输入长度5~16的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 5, max: 16, message: '请输入长度5~16的字符', trigger: 'blur' }
  ],
  rePassword: [
    //checkRePassword函数需要写道rules上面否则不能用
    {validator:checkRePassword, trigger: 'blur' }
  ]
}
//获取form实例做统一校验
const form = ref(null)
const doRegister = () =>{
    //解构赋值
    const { username, password} = registerData.value
    //调用实例方法
    form.value.validate(async (valid) =>{
            //valid:所有表单项都通过校验才为true
            if(valid){
                const res = await userRegisterAPI({ username, password})
                if(res.data.code===0){
                    //注册成功
                    ElMessage.success(res.data.message ? res.data.message : '注册成功')
                    router.push('/login')
                }else{
                    //失败
                    ElMessage.error('注册失败')
                }
            }
            }
    )
}
//将user添加到pinia中
import { useUserStore } from '@/stores/userStore';
const userStore = useUserStore()

import router from '@/router';

const doLogin = () =>{
    //解构赋值
    const { username, password} = loginData.value
    //调用实例方法
    form.value.validate(async (valid) =>{
            //valid:所以表单项都通过校验才为true
            if(valid){
                await userStore.getUserInfo({ username, password})
                if(userStore.userInfo.code===0){
                    //登录成功
                    ElMessage.success('登录成功')
                    router.push('/')
                }else{
                    //失败
                    ElMessage.error(userStore.userInfo.message ? userStore.userInfo.message : '登录失败')
                }
            }
            }
    )
}

</script>


<template>
    <div>
        <header class="login-header">
            <div class="container m-top-20">
                <h1 class="logo">
                    <RouterLink to="/">捷贸云</RouterLink>
                </h1>
                <RouterLink class="entry" to="/">
                    进入网站首页
                    <i class="iconfont icon-shuangjiantou-jinru"></i>
                </RouterLink>
            </div>
        </header>
        <section class="login-section">

            <!-- 注册表单 -->
            <div class="wrapper" v-if="isRegister"  >
                <nav>
                    <a href="javascript:;">账户注册</a>
                </nav>
                <div class="account-box">
                    <div class="form">
                        <el-form ref="form" :model="registerData" :rules="rules" label-position="right" label-width="60px" status-icon :hide-required-asterisk="true">
                            <el-form-item label="账户" prop="username">
                                <el-input placeholder="请输入用户名" v-model="registerData.username"/>
                            </el-form-item>
                            <el-form-item label="密码" prop="password">
                                <el-input :show-password="true" type="password" placeholder="请输入密码" v-model="registerData.password"/>
                            </el-form-item>
                            <el-form-item prop="rePassword">
                                <el-input :show-password="true" type="password" placeholder="请再次输入密码" v-model="registerData.rePassword"/>
                            </el-form-item>
                            <el-button size="large" class="subBtn" @click="doRegister">点击注册</el-button>
                        </el-form>
                        <el-form-item class="flex">
                            <el-link type="info" :underline="false" @click="isRegister = false">
                                ← 返回
                            </el-link>
                        </el-form-item>
                    </div>
                </div>
            </div>

            <!-- 登录表单 -->
            <div class="wrapper" v-else >
                <nav>
                    <a href="javascript:;">账户登录</a>
                </nav>
                <div class="account-box">
                    <div class="form">
                        <el-form ref="form" :model="loginData" :rules="rules" label-position="right" label-width="60px" :hide-required-asterisk="true" status-icon>
                            <el-form-item label="账户" prop="username">
                                <el-input placeholder="请输入用户名" v-model="loginData.username"/>
                            </el-form-item>
                            <el-form-item label="密码" prop="password">
                                <el-input :show-password="true" type="password" placeholder="请输入密码" v-model="loginData.password"/>
                            </el-form-item>
                            <el-form-item label-width="0px" class="flex">
                                <div class="flex">
                                    <el-checkbox>我已同意隐私条款和服务条款</el-checkbox>
                                    <el-link type="primary" :underline="false">忘记密码？</el-link>
                                </div>
                            </el-form-item>

                            <el-button size="large" class="subBtn" @click="doLogin">点击登录</el-button>
                        </el-form>
                        <el-form-item class="flex">
                                <el-link type="info" :underline="false" @click="isRegister = true">
                                    注册 →
                                </el-link>
                        </el-form-item>
                    </div>
                </div>
            </div>
        </section>

        <footer class="login-footer">
            <div class="container">
                <p>
                    <a href="javascript:;">关于我们</a>
                    <a href="javascript:;">帮助中心</a>
                    <a href="javascript:;">售后服务</a>
                    <a href="javascript:;">配送与验收</a>
                    <a href="javascript:;">商务合作</a>
                    <a href="javascript:;">搜索推荐</a>
                    <a href="javascript:;">友情链接</a>
                </p>
                <p>CopyRight &copy; 捷贸云</p>
            </div>
        </footer>
    </div>
</template>

<style scoped lang='scss'>
.login-header {
    background: #fff;
    border-bottom: 1px solid #e4e4e4;

    .container {
        display: flex;
        align-items: flex-end;
        justify-content: space-between;
    }

    .logo {
        width: 200px;

        a {
            display: block;
            height: 132px;
            width: 100%;
            text-indent: -9999px;
            background: url("@/assets/images/logo.gif") no-repeat center 18px / contain;
        }
    }

    .sub {
        flex: 1;
        font-size: 24px;
        font-weight: normal;
        margin-bottom: 38px;
        margin-left: 20px;
        color: #666;
    }

    .entry {
        width: 120px;
        margin-bottom: 38px;
        font-size: 16px;

        i {
            font-size: 14px;
            color: $xtxColor;
            letter-spacing: -5px;
        }
    }
}

.login-section {
    background: url('@/assets/images/login-bg.png') no-repeat center / cover;
    height: 488px;
    position: relative;

    .wrapper {
        width: 380px;
        background: #fff;
        position: absolute;
        left: 55%;
        top: 54px;
        transform: translate3d(100px, 0, 0);
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

        nav {
            font-size: 14px;
            height: 55px;
            margin-bottom: 20px;
            border-bottom: 1px solid #f5f5f5;
            display: flex;
            padding: 0 40px;
            text-align: right;
            align-items: center;

            a {
                flex: 1;
                line-height: 1;
                display: inline-block;
                font-size: 18px;
                position: relative;
                text-align: center;
            }
        }
    }
}

.login-footer {
    padding: 30px 0 50px;
    background: #fff;

    p {
        text-align: center;
        color: #999;
        padding-top: 20px;

        a {
            line-height: 1;
            padding: 0 10px;
            color: #999;
            display: inline-block;

            ~a {
                border-left: 1px solid #ccc;
            }
        }
    }
}

.account-box {
    .toggle {
        padding: 15px 40px;
        text-align: right;

        a {
            color: $xtxColor;

            i {
                font-size: 14px;
            }
        }
    }

    .form {
        padding: 0 20px 0 20px;


        &-item {
            margin-bottom: 28px;

            .input {
                position: relative;
                height: 36px;

                >i {
                    width: 34px;
                    height: 34px;
                    background: #cfcdcd;
                    color: #fff;
                    position: absolute;
                    left: 1px;
                    top: 1px;
                    text-align: center;
                    line-height: 34px;
                    font-size: 18px;
                }

                input {
                    padding-left: 44px;
                    border: 1px solid #cfcdcd;
                    height: 36px;
                    line-height: 36px;
                    width: 100%;

                    &.error {
                        border-color: $priceColor;
                    }

                    &.active,
                    &:focus {
                        border-color: $xtxColor;
                    }
                }

                .code {
                    position: absolute;
                    right: 1px;
                    top: 1px;
                    text-align: center;
                    line-height: 34px;
                    font-size: 14px;
                    background: #f5f5f5;
                    color: #666;
                    width: 90px;
                    height: 34px;
                    cursor: pointer;
                }
            }

            >.error {
                position: absolute;
                font-size: 12px;
                line-height: 28px;
                color: $priceColor;

                i {
                    font-size: 14px;
                    margin-right: 2px;
                }
            }
        }

        .agree {
            a {
                color: #069;
            }
        }

        .btn {
            display: block;
            width: 100%;
            height: 40px;
            color: #fff;
            text-align: center;
            line-height: 40px;
            background: $xtxColor;

            &.disabled {
                background: #cfcdcd;
            }
        }

        .flex {
            left: 0px;
            width: 100%;
            display: flex;
            justify-content: space-between;
        }
    }

    .action {
        padding: 20px 40px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .url {
            a {
                color: #999;
                margin-left: 10px;
            }
        }
    }
}

.subBtn {
    background: $xtxColor;
    width: 100%;
    color: #fff;
}
</style>